// 邮箱正则判断
function checkMobile(str) {
	var re = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/
	if (!re.test(str)) alert("邮箱未输入或错误");
	return str
}

// 倒计时
function djs() {
	var time = 30
	//设置一个定时，一秒执行一次
	var mytime = setInterval(function() {
		subs();
	}, 1000)

	function subs() {
		time--;
		$('#bt').attr("value", "请" + time + "秒后再试");
		if (time === 0) {
			clearInterval(mytime);
			$('#bt').attr("value", "发送验证码");
			$('#bt').attr("disabled", false); //按键可用
		} else {
			$('#bt').attr("disabled", true); //按键不可用
		}
	}
}

let ip_ = 'http://192.168.0.111:3000'

// 切换选项卡
let box = document.getElementsByClassName('box')
let btn = document.getElementsByClassName('btn')
let yzm = localStorage.getItem('yzm')

// 如果已登录 验证码判断注册/登陆组件是否开启
if (yzm) {
	for (let i = 0; i < box.length; i++) {
		$('.box4').removeClass('box')
		$('.box4').css({
			display: 'none'
		})
		$('.login').addClass('box')
	}
}

// 未登陆正常选项卡切换 + 注册/登陆界面
for (let i = 0; i < btn.length; i++) {
	btn[i].addEventListener('touchstart', function() {
		for (let j = 0; j < box.length; j++) {
			box[j].style.display = 'none'
		}
		box[i].style.display = 'block'
	})
}

// 点击登陆/注册
$('.button').on('tap', function() {
	// 必须输入账号密码
	if ($('.name').val() !== '' && $('.password').val() !== '') {
		$.ajax({
			type: 'get',
			url: 'http://192.168.0.111:3000/users/sign',
			data: {
				name: $('.name').val(),
				password: $('.password').val(),
				email: checkMobile($('.email').val())
			},
			success(res) {
				console.log(res);
				// code = 404 此用户已存在
				if (res.code == 404) {
					$.ajax({
						type: 'get',
						url: 'http://192.168.0.111:3000/users/login',
						data: {
							name: $('.name').val(),
							password: $('.password').val(),
							email: $('.email').val()
						},
						success(res) {
							console.log(res.data);
							let yzm = localStorage.getItem('yzm')
							// res.code = 200 登陆成功
							if ($('.email123').val() == yzm && res.data.code == 200) {
								alert('登陆成功');
								// 打开用户界面
								$('.login').css({
									display: 'block'
								})
								// 关闭登陆注册界面
								$('.box4').css({
									display: 'none'
								})
								// 存储邮箱，放入用户名
								localStorage.setItem('email', $('.email').val())
								let email = localStorage.getItem('email')
								let module = `<span class="spn1">${email}</span>`
								$('#right_a').append(module)
								for (let i = 0; i < btn.length; i++) {
									if (yzm) {
										for (let j = 0; j < box.length; j++) {
											box[j].style.display = 'none'
										}
										$('.box4').removeClass('box')
										$('.login').addClass('box')
										$('.login').css({
											display: 'block'
										})
									}
								}
							} else if ($('.email123').val() == '') {
								alert('验证码未输入')
							} else if ($('.email123').val() !== yzm) {
								alert('验证码输入错误')
							} else if (res.data.code == 404) {
								alert(res.data.msg)
							} else if (res.code == 401) {
								alert(res.data.msg)
							}
						}
					})
				} else if ($('.email').val() == '') {
					alert('邮箱未输入')
				} else if ($('.email123').val() == '') {
					alert('验证码未输入')
				} else {
					alert('注册成功')
				}
			}
		})
	} else {
		alert('账号或密码未输入')
	}
})

// 如果用户已登录 获取邮箱 放入用户名中
let email = localStorage.getItem('email')
if (email) {
	let module = `<span class="spn1">${email}</span>`
	$('#right_a').append(module)
}

// 修改用户名
let name = localStorage.getItem('name')
let img = JSON.parse(localStorage.getItem('img'))
if (name !== null || img !== null) {
	let module = `
	<img class="img2" src="${img?img:'./images/avatar.png'}" alt="">
	<span class="spn1">${name}</span>
	`
	$('#right_a').html(module)
}

// 点击获取验证码+邮箱正则判断
$('#bt').on('tap', function() {
	if ($('.email').val() !== '') {
		$.ajax({
			type: 'get',
			url: 'http://192.168.0.111:3000/emails/email',
			data: {
				email: checkMobile($('.email').val())
			},
			success(res) {
				let re = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/
				if (re.test($('.email').val())) {
					console.log(res);
					djs()
					localStorage.setItem('yzm', res.data)
				}
			}
		})
	} else {
		alert('邮箱未输入')
	}
})


// 点击退出登陆
$('.btn3').on('tap', function() {
	$('.spn1').remove()
	localStorage.clear()
	$('.login').removeClass('box')
	$('.login').css({
		display: 'none'
	})
	$('.box4').addClass('box').
	css({
		display: 'block'
	})
})


// 点击头像 修改用户名+上传头像
$('#right_a').on('tap', function() {
	location.href = 'html/user.html'
})

// 获取推荐文章接口
mui.ajax('http://192.168.0.111:3000/article/search', {
	dataType: 'json', //服务器返回json格式数据
	type: 'get', //HTTP请求类型	              
	success: function(res) {
		//服务器返回响应，根据响应结果，分析是否登录成功；
		let data = res.data
		data.forEach(item => {
			let module = `
			<li class="mui-table-view-cell item">
				<a href="./html/articles.html?id=${item.id}">
					<div class="item1">
						<img src="${item.img}" alt="">
						<div class="item_title" style="display:flex; flex-direction: column;
			justify-content: space-between;">
							<span class="spntwo">${item.title}</span>
							<span class="spnone">${item.time}</span>
						</div>
					</div>
				</a>
			</li>
			`
			$('.li_item').append(module)
		})
		$('.item').on('tap', function() {
			let history = $(this).find('.spntwo').text()
			let str = localStorage.getItem('history')
			if (str == null) {
				localStorage.setItem('history', history)
				str = ['']
			} else {
				localStorage.setItem('history', str + '?' + history)
			}
			console.log(str);
		})
	},
	error: function(xhr, type, errorThrown) {
		//异常处理；
		console.log(type);
	}
});


// 点击推荐文章 => 更多
$('.wenzhang').on('tap', function() {
	location.href = 'html/money.html'
})

// 学习手册接口
student()

function student() {
	mui.ajax('http://192.168.0.111:3000/knowledge/know', {
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型	              
		success: function(res) {
			let data = res.data
			data.forEach(item => {
				let module = `
					<div class="cont1">
						<img src="${item.img}" alt="">
						<h4>${item.title}</h4>
						<p>${item.time}</p>
					</div>
				`
				$('#box_content').append(module)
			})
			// 点击进入详情页
			$('.cont1').on('tap', function() {
				let title = this.children[1].innerText
				localStorage.setItem('title', title)
				location.href = './html/know.html'
			})
		},
		error: function(xhr, type, errorThrown) {
			//异常处理；
			console.log(type);
		}
	});
}

// 点击党史知识 更多进入 更多详情页
$('.bnt_3').on('tap', function() {
	window.location.href = './html/moneyknow.html'
})

// 党建参阅 渲染
mui.ajax('http://192.168.0.111:3000/refers/re', {
	dataType: 'json', //服务器返回json格式数据
	type: 'get', //HTTP请求类型	              
	success: function(res) {
		let data = res.data
		let arr = data.splice(0, 4)
		arr.forEach(item => {
			let module = `
				<li class="mui-table-view-cell s_btn" style="height:80px; position: relative">
				    <a class="mui-navigate-right" style="padding-bottom: 30px;" href="html/ref.html?id=${item.id}">
						<span><strong>${item.title}</strong></span>
						<p style="position: absolute; bottom: -5px;">${item.time}</p>
					</a>
				</li>
				`
			$('.item_tt').append(module)
		})
	},
	error: function(xhr, type, errorThrown) {
		//异常处理；
		console.log(type);
	}
});


// 点击党建参阅 更多进入 更多详情页
$('.bnt_4').on('tap', function() {
	window.location.href = './html/moneyref.html'
})




// 先模风采················

// 获取按钮元素
let menuBtn = document.getElementById('menuBtn');
let a = document.querySelector('#menuBtn a');
// 绑定点击事件
menuBtn.addEventListener('tap', function() {
	// 显示菜单
	mui('#menu').popover('toggle');
});
// 点击li 切换文本 并且匹配相对于的人员
$('.li_ite').on('tap', function() {
	// 点击赋值
	a.innerHTML = $(this).text()
	mui('#menu').popover('hide');
	mui.ajax('http://192.168.0.111:3000/branchs/bran', {
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型	              
		success: function(res) {
			let data = res.data
			$('.ch_r').html('')
			data.forEach(item => {
				// 通过相对应的地区，获取人员
				if (a.innerText == item.area) {
					let module = `
					<li class="mui-table-view-cell mui-media">
					    <a href="html/dzb.html?id=${item.id}">
					        <img class="mui-media-object mui-pull-left" style="max-width:65px;" src="${item.img}">
					        <div class="mui-media-body">
					            ${item.name}
					            <p class='mui-ellipsis'>${item.area}&nbsp;&nbsp;|&nbsp;&nbsp;${item.Political}</p>
					            <p class='mui-ellipsis'>${item.h2}:</p>
					            <p class='mui-ellipsis'>${item.Personal}</p>
					        </div>
					    </a>
					</li>
				`
					$('.ch_r').append(module)
				} else if (a.innerHTML == '财源') {
					$('.ch_r').html('')
				} else if (a.innerText == '所有党支部') {
					let module = `
					<li class="mui-table-view-cell mui-media">
					    <a href="html/dzb.html?id=${item.id}">
					        <img class="mui-media-object mui-pull-left" style="max-width:65px;" src="${item.img}">
					        <div class="mui-media-body">
					            ${item.name}
					            <p class='mui-ellipsis'>${item.area}&nbsp;&nbsp;|&nbsp;&nbsp;${item.Political}</p>
					            <p class='mui-ellipsis'>${item.h2}:</p>
					            <p class='mui-ellipsis'>${item.Personal}</p>
					        </div>
					    </a>
					</li>
					`
					$('.ch_r').append(module)
				}
			})
		},
		error: function(xhr, type, errorThrown) {
			//异常处理；
			console.log(type);
		}
	});
})
// 点击取消
$('.li_ab').on('tap', function() {
	mui('#menu').popover('hide');
})

// 调用党支部接口 渲染页面
dzb()

function dzb() {
	mui.ajax('http://192.168.0.111:3000/branchs/bran', {
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型	              
		success: function(res) {
			let data = res.data
			data.forEach(item => {
				let module = `
				<li class="mui-table-view-cell mui-media">
				    <a href="html/dzb.html?id=${item.id}">
				        <img class="mui-media-object mui-pull-left" style="max-width:65px;" src="${item.img}">
				        <div class="mui-media-body">
				            ${item.name}
				            <p class='mui-ellipsis'>${item.area}&nbsp;&nbsp;|&nbsp;&nbsp;${item.Political}</p>
				            <p class='mui-ellipsis'>${item.h2}:</p>
				            <p class='mui-ellipsis'>${item.Personal}</p>
				        </div>
				    </a>
				</li>
			`
				$('.ch_r').append(module)
			})
		},
		error: function(xhr, type, errorThrown) {
			//异常处理；
			console.log(type);
		}
	});
}


// 点击修改密码
$('.but_xg').on('tap', function() {
	window.location.href = './html/xgpassword.html'
})

// 点击浏览历史
$('.but_his').on('tap', function() {
	window.location.href = './html/history.html'
})